import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


const routes = [
    {
        path: '/login',
        name: 'login',
        component: () => import(/* webpackChunkName: "about" */ '../views/login.vue'),
        meta: {
            title: "登录",
            hidden: true,
        }
    },
    {
        path: '/',
        name: 'lay',
        meta: {
            title: "布局",
            hidden: true
        },
        component: () => import(/* webpackChunkName: "about" */ '../lay/index.vue'),
        children: [
            {
                path: "",
                redirect: "newsList",
                meta: {
                    title: "重定向",
                    hidden: true
                }
            },
            {
                path: 'newsList',
                name: 'newsList',
                component: () => import(/* webpackChunkName: "about" */ '../views/newsList.vue'),
                meta: {
                    title: "新闻列表",
                    icon: require("../assets/NavBar/list.png"),
                    icon_sel: require("../assets/NavBar/list_sel.png")
                }
            },
            {
                path: 'newsType',
                name: 'newsType',
                component: () => import(/* webpackChunkName: "about" */ '../views/newsType.vue'),
                meta: {
                    title: "新闻分类",
                    icon: require("../assets/NavBar/type.png"),
                    icon_sel: require("../assets/NavBar/type_sel.png")
                }
            },
            {
                path: 'user',
                name: 'user',
                component: () => import(/* webpackChunkName: "about" */ '../views/user.vue'),
                meta: {
                    title: "用户管理",
                    icon: require("../assets/NavBar/user.png"),
                    icon_sel: require("../assets/NavBar/user_sel.png")
                }
            },
        ]
    },
    {
        path: '/register',
        name: 'register',
        component: () => import(/* webpackChunkName: "about" */ '../views/register.vue'),
        meta: {
            title: "注册",
            hidden: true,
        }
    }
]


const router = new VueRouter({
    routes,
    linkActiveClass: "active",
})


router.beforeEach((to, from, next) => {
    window.document.title = to.meta.title;
    if (localStorage.getItem("token")) {
        next()
    } else {
        if (to.path == "/login" || to.path == "/register") {
            next()
        } else {
            next("/login")
        }
    }
})
export default router
